package com.pioneer.controller.archive;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.archive.domain.ArcStore;
import com.pioneer.web.archive.service.IArcStoreService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 仓库管理
 *
 * @author hlm
 */
@RestController
@RequestMapping("/archive/store")
public class ArcStoreController extends BaseController {

    @Resource
    private IArcStoreService storeService;

    /**
     * 获取仓库管理列表
     *
     * @param store 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('archive:store:list')")
    @GetMapping("/list")
    public AjaxResult list(ArcStore store) {
        boolean isPage = startPage();
        List<ArcStore> list = storeService.list(Wrappers.query(store));
        return success(isPage, list);
    }

    /**
     * 根据仓库管理id获取详细信息
     *
     * @param storeId 仓库管理id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('archive:store:query')")
    @GetMapping(value = "/{storeId}")
    public AjaxResult getInfo(@PathVariable Long storeId) {
        return AjaxResult.success(storeService.getById(storeId));
    }

    /**
     * 新增仓库管理
     *
     * @param store 仓库管理
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('archive:store:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ArcStore store) {
        if (storeService.checkCodeUnique(store)) {
            return AjaxResult.error("仓库编码【" + store.getStoreCode() + "】已存在，请重新输入");
        }
        return toAjax(storeService.save(store));
    }

    /**
     * 修改仓库管理
     *
     * @param store 仓库管理
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('archive:store:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ArcStore store) {
        if (storeService.checkCodeUnique(store)) {
            return AjaxResult.error("仓库编码【" + store.getStoreCode() + "】已存在，请重新输入");
        }
        return toAjax(storeService.updateById(store));
    }

    /**
     * 删除仓库管理
     *
     * @param storeIds 仓库管理id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('archive:store:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storeIds}")
    public AjaxResult remove(@PathVariable Long[] storeIds) {
        return toAjax(storeService.removeByIds(Arrays.asList(storeIds)));
    }
}
